﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
using System.Windows.Media;

namespace TestDxfDocument
{

    class _Standard1115_2
    {
        public static void Standard1115_2(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
                                                                           //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            var D = 50.0;//切削直径
            try
            {
                D = Convert.ToDouble(data["dc"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[D] not found");
            }
            var d = 22.0;//连接直径
            try
            {
                d = Convert.ToDouble(data["dcon"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[d] not found");
            }
            var L = 50.0;//最小切削宽度
            try
            {
                L = Convert.ToDouble(data["cwn"]);
            }
            catch (Exception e)
            {
                Console.WriteLine("[L] not found");
            }
            //坐标
            var v0 = new Vector2(0, 0);
            var v1 = fun.Polar(v0, 90 * pi / 180, D / 2);
            var v2 = fun.Polar(v1, 0, L / 2);
            var v3 = fun.Polar(v2, -90 * pi / 180, D);
            var v4 = new Vector2(v1.X, v3.Y);
            var v5 = fun.Polar(v3, 90 * pi / 180, 0.1484 * D);
            var v6 = new Vector2(v4.X, v5.Y);
            var v7 = fun.Polar(v3, 90 * pi / 180, 0.2725 * D);
            var v8 = new Vector2(v4.X, v7.Y);
            var v0p = fun.Polar(v0, 0, L / 2);
            var v9 = fun.Polar(v0p, pi, 0.046 * L / 2);
            var v10 = fun.mirrorpoint(v9, fun.Polar(v0, 0, L / 2 / 2), fun.Polar(fun.Polar(v0, 0, L / 2 / 2), -90 * pi / 180, 1));
            var v11 = fun.Polar(v9, -90 * pi / 180, 0.1722 * D);
            var v12 = fun.mirrorpoint(v11, fun.Polar(v0, 0, L / 2 / 2), fun.Polar(fun.Polar(v0, 0, L / 2 / 2), -90 * pi / 180, 1));
            var v13 = fun.Polar(v9, pi, 0.2297 * L / 2);
            var v14 = new Vector2(v13.X, v11.Y);
            var v15 = fun.Polar(v14, -90 * pi / 180, (v14.Y - v7.Y) / 2);
            var v16 = fun.mirrorpoint(v15, fun.Polar(v0, 0, L / 2 / 2), fun.Polar(fun.Polar(v0, 0, L / 2 / 2), -90 * pi / 180, 1));
            var v17 = fun.mirrorpoint(v14, fun.Polar(v0, 0, L / 2 / 2), fun.Polar(fun.Polar(v0, 0, L / 2 / 2), -90 * pi / 180, 1));
            var v18 = fun.mirrorpoint(v13, fun.Polar(v0, 0, L / 2 / 2), fun.Polar(fun.Polar(v0, 0, L / 2 / 2), -90 * pi / 180, 1));
            var v19 = fun.Polar(v0, pi, 5);
            var v20 = fun.Polar(v0, 0, L+5);



            var polyline1 = Pus.AddPointsToLwPolyLine(v1, v2, v3, v4, v1);
            var polyline2 = Pus.AddPointsToLwPolyLine(v10, v12, v8);
            var polyline3 = Pus.AddPointsToLwPolyLine(v9, v11, v7);
            var polyline4 = Pus.AddPointsToLwPolyLine(v18, v16, v15, v13);
            var polyline5 = Pus.Fillet4LwPline(polyline4, 1, v17.Y - v16.Y);
            var polyline6 = Pus.Fillet4LwPline(polyline5, 3, v17.Y - v16.Y);
            fun.PolyFill(dxf, polyline1, 0.2, contour);
            fun.PolyFill(dxf, polyline2, 0.2, contour);
            fun.PolyFill(dxf, polyline3, 0.2, contour);
            fun.PolyFill(dxf, polyline6, 0.2, contour);


            Line line1 = new Line(v19, v20);
            line1.Layer = centre;
            line1.Lineweight = Lineweight.W25;
            line1.Linetype = Linetype.DashDot;
            dxf.AddEntity(line1);


            var line2 = new Line(v8, v7);
            fun.drawBline(dxf, line2, 0.2, contour);
            var line3 = new Line(v6, v5);
            fun.drawBline(dxf, line3, 0.2, contour);
            var line4 = new Line(v12, v17);
            fun.drawBline(dxf, line4, 0.2, contour);
            var line5 = new Line(v14, v11);
            fun.drawBline(dxf, line5, 0.2, contour);

            var hatchpoly1 = Pus.AddPointsToLwPolyLine(v1, v2, v0p, v0, v1);
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, -30, 80, hline1, harc1, contour);

            var hatchpoly2 = Pus.AddPointsToLwPolyLine(v8, v7, v5, v6, v8);
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 20, hline2, harc2, hatching);

            var v21 = fun.Polar(v20, 0, 1.5 * D);
            var v22 = fun.Polar(v21, pi, 0.6 * D);
            var v23 = fun.Polar(v21, 0, 0.6 * D);
            var v24 = fun.Polar(v21, 90 * pi / 180, 0.6 * D);
            var v25 = fun.Polar(v21, -90 * pi / 180, 0.6 * D);
            Line line6 = new Line(v22, v23);
            line6.Layer = centre;
            line6.Lineweight = Lineweight.W25;
            line6.Linetype = Linetype.DashDot;
            dxf.AddEntity(line6);
            Line line7 = new Line(v24, v25);
            line7.Layer = centre;
            line7.Lineweight = Lineweight.W25;
            line7.Linetype = Linetype.DashDot;
            dxf.AddEntity(line7);
            var polyline7 = Pus.AddPointsToLwPolyLine(fun.Polar(v21, pi, D / 2), fun.Polar(v21, 0, D / 2), fun.Polar(v21, pi, D / 2));
            polyline7.Vertexes[0].Bulge = Math.Tan(180 * Math.PI / 180 / 4);
            polyline7.Vertexes[1].Bulge = Math.Tan(180 * Math.PI / 180 / 4);
            fun.PolyFill(dxf, polyline7, 0.2, contour);
            var v26 = new Vector2(v21.X, v7.Y);
            var v27 = fun.Polar(v26, pi, 0.0487 * D);
            var v28 = fun.Polar(v26, 0, 0.0487 * D);
            var v29 = new Vector2(v21.X - fun.CalDistance(v27, v26), v21.Y - (Math.Sqrt(Math.Pow(0.1855 * D, 2) - Math.Pow(fun.CalDistance(v27, v26), 2))));
            var v30 = fun.mirrorpoint(v29, v21, v26);
            var v31 = new Vector2(v21.X - fun.CalDistance(v27, v26), v21.Y - (Math.Sqrt(Math.Pow(0.1516 * D, 2) - Math.Pow(fun.CalDistance(v27, v26), 2))));
            var v32 = fun.mirrorpoint(v31, v21, v26);

            var polyline8 = Pus.AddPointsToLwPolyLine(v29, v30);
            polyline8.Vertexes[0].Bulge = -Math.Tan((360 * pi / 180 - (fun.CalAngle(v21, v30) - fun.CalAngle(v21, v29))) / 4);
            fun.PolyFill(dxf, polyline8, 0.2, contour);
            var polyline9 = Pus.AddPointsToLwPolyLine(v31, v32, v28, v27, v31);
            polyline9.Vertexes[0].Bulge = -Math.Tan((360 * pi / 180 - (fun.CalAngle(v21, v32) - fun.CalAngle(v21, v31))) / 4);
            fun.PolyFill(dxf, polyline9, 0.2, contour);

            var v34 = fun.Polar(v21, 90 * pi / 180, D / 2);
            var v35 = fun.Polar(v21, 38 * pi / 180, 0.3425 * D);
            var v36 = fun.Polar(v21, 45 * pi / 180, D / 2);
            var v37 = fun.Polar(v21, -7 * pi / 180, 0.3425 * D);
            var v38 = fun.Polar(v21, 0 * pi / 180, D / 2);
            var v39 = fun.Polar(v21, -52 * pi / 180, 0.3425 * D);
            var v40 = fun.Polar(v21, -45 * pi / 180, D / 2);
            var v41 = fun.Polar(v21, -97 * pi / 180, 0.3425 * D);
            var v42 = fun.Polar(v21, -90 * pi / 180, D / 2);
            var v43 = fun.Polar(v21, -142 * pi / 180, 0.3425 * D);
            var v44 = fun.Polar(v21, -135 * pi / 180, D / 2);
            var v45 = fun.Polar(v21, 173 * pi / 180, 0.3425 * D);
            var v46 = fun.Polar(v21, 180 * pi / 180, D / 2);
            var v47 = fun.Polar(v21, 128 * pi / 180, 0.3425 * D);
            var v48 = fun.Polar(v21, 135 * pi / 180, D / 2);
            var v33 = fun.Polar(v21, 83 * pi / 180, 0.3425 * D);


            var polyline10 = Pus.AddPointsToLwPolyLine(v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v33, v34);
            polyline10.Vertexes[0].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[1].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[2].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[3].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[4].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[5].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[6].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[7].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[8].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[9].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[10].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[11].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[12].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[13].Bulge = Math.Tan(35 * pi / 180 / 4);
            polyline10.Vertexes[14].Bulge = -Math.Tan(54 * pi / 180 / 4);
            polyline10.Vertexes[15].Bulge = Math.Tan(35 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline10, 0.2, contour);

            fun.drawBline(dxf, fun.mirrorline(new Line(v1, v2), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v1, v4), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v3, v2), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v3, v4), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v5, v6), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v7, v8), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v9, v11), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v10, v12), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v14, v11), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v17, v12), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v8, v12), v3, v5), 0.2, contour);
            fun.drawBline(dxf, fun.mirrorline(new Line(v7, v11), v3, v5), 0.2, contour);

            Vector2 vm0 = fun.mirrorpoint(v0, v3, v5);
            Vector2 vm1 = fun.mirrorpoint(v1, v3, v5);
            Vector2 vm6 = fun.mirrorpoint(v6, v3, v5);
            Vector2 vm8 = fun.mirrorpoint(v8, v3, v5);
            Vector2 vm13 = fun.mirrorpoint(v13, v3, v5);
            Vector2 vm15 = fun.mirrorpoint(v15, v3, v5);
            Vector2 vm16 = fun.mirrorpoint(v16, v3, v5);
            Vector2 vm18 = fun.mirrorpoint(v18, v3, v5);

            //连接点
            Vector2 va1 = new Vector2(v9.X, v5.Y);
            Vector2 va2 = new Vector2(v9.X, (v5.Y+v7.Y)/2);
            Vector2 vam1 = fun.mirrorpoint(va1,v5,v7);
            Vector2 vam2 = fun.mirrorpoint(va2,v5,v7);

            var polyline11 = Pus.AddPointsToLwPolyLine(va1,va2,vam2,vam1);
            fun.PolyFill(dxf, polyline11, 0.2, contour);



            var hatchpolym1 = Pus.AddPointsToLwPolyLine(vm1, v2, v0p, vm0, vm1);
            String[] htypem1 = new String[100];
            Line[] hlinem1 = new Line[100];
            Arc[] harcm1 = new Arc[100];
            int hnumstm1 = hatchpolym1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumstm1; i++)
            {
                htypem1[i] = hatchpolym1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htypem1[i] == "Line")
                {
                    hlinem1[i] = (Line)hatchpolym1.Explode()[i];
                }
                if (htypem1[i] == "Arc")
                {
                    harcm1[i] = (Arc)hatchpolym1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htypem1, hnumstm1, 30, 80, hlinem1, harcm1, contour);

            var hatchpolym2 = Pus.AddPointsToLwPolyLine(v5, vm6, vm8, v7, v5);
            String[] htypem2 = new String[100];
            Line[] hlinem2 = new Line[100];
            Arc[] harcm2 = new Arc[100];
            int hnumstm2 = hatchpolym2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumstm2; i++)
            {
                htypem2[i] = hatchpolym2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htypem2[i] == "Line")
                {
                    hlinem2[i] = (Line)hatchpolym2.Explode()[i];
                }
                if (htypem2[i] == "Arc")
                {
                    harcm2[i] = (Arc)hatchpolym2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htypem2, hnumstm2, -45, 20, hlinem2, harcm2, hatching);


            var polylinem4 = Pus.AddPointsToLwPolyLine(vm18, vm16, vm15, vm13);
            var polylinem5 = Pus.Fillet4LwPline(polylinem4, 1, v17.Y - v16.Y);
            var polylinem6 = Pus.Fillet4LwPline(polylinem5, 3, v17.Y - v16.Y);
            fun.PolyFill(dxf, polylinem6, 0.2, contour);




            var height = 2.5;//字体高度
            var offset = 10;//偏移量
            var arrowsize = 1;//箭头大小
            var exline = 1;//延伸线长度


            fun.dimrot1(dxf, v1, v4, 90, offset * 2, 0, height, arrowsize, exline, "DC=" + D, "", "");
            fun.dimrot1(dxf, v6, vm6, 180, offset * 2, 0, height, arrowsize, exline, "CWN=" + L, "", "");
            fun.dimrot1(dxf, fun.Polar(v21, pi, 0.1516 * D), fun.Polar(v21, 0, 0.1516 * D), 0, D, 0, height, arrowsize, exline, "DCON=" + d, "", "");



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - offset * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

    }
}
